import store from "@/store";
import define from "./define";
// const host = define.baseURL
const defaultOpt = {
  load: true,
};

function request(config) {
  const sysConfigInfo = uni.getStorageSync("sysConfigInfo");
  if (!sysConfigInfo) {
    uni.reLaunch({
      url: "/pages/login/index",
    });
    return;
  }
  const host = sysConfigInfo.commonUrl;
  config.options = Object.assign(defaultOpt, config.options);
  const token = uni.getStorageSync("token") || "";
  let header = {
    "Content-Type": "application/json;charset=UTF-8",
    "jnpf-origin": "app",
    ...config.header,
  };
  if (token) header["Authorization"] = token;
  let url = config.url.indexOf("http") > -1 ? config.url : host + config.url;
  if (config.url === "/api/oauth/Login") {
    url +=
      "?client_id=admin&client_secret=123456&scope=all&grant_type=password";
  }
  if (config.options.load) {
    uni.showLoading({
      title: config.options.loadText || "正在加载",
    });
  }

  // console.log('config.data==============',config.data)
  // console.log('config.url==============',url)
  // console.log('config.header==============',header)

  return new Promise((resolve, reject) => {
    uni.request({
      url: url,
      data: config.data || null,
      method: config.method || "GET",
      header: header,
      timeout: 100000,
      success: (res) => {
        if (config.options.load) {
          uni.hideLoading();
        }
        if (res.statusCode === 200) {
          if (res.data.code == 200) {
            resolve(res.data);
          } else {
            ajaxError(res.data);
            reject(res.data.msg);
          }
        } else {
          ajaxError(res.data);
          reject(res.errMsg);
        }
      },
      fail: (err) => {
        // reject(err,'err')
        if (config.options.load) {
          uni.hideLoading();
        }
        reject(err);
      },
    });
  });
}

function ajaxError(data) {
  uni.showToast({
    title: data.msg || "请求出错，请重试",
    icon: "none",
    complete() {
      if (data.code === 600 || data.code === 601 || data.code === 602) {
        setTimeout(() => {
          store.dispatch("user/resetToken").then(() => {
            uni.reLaunch({
              url: "/pages/login/index",
            });
          });
        }, 1500);
      }
    },
  });
}

export default request;
